#if !SERVER
using ActorSystem;
using Net.Client;
using Net.Share;
using UnityEngine;

namespace Example2
{
    public partial class Player : Actor, IActorOnCollisionEnter, IActorOnCollisionExit
    {
        public ActorView bullet;
        public Transform firePoint;
        public bool isGround = true;
        internal Rigidbody rigidbody;
        public float jumpSpeed = 500f;

        protected override void EnterArea(in Operation opt)
        {
            base.EnterArea(opt);
            Property = new NormalProperty();
            headBloodBar = Object.Instantiate(GameManager.I.headBloodBar, GameManager.I.UIRoot);
            headBloodBar.target = View.transform;
            headBloodBar.offset = headBarOffset;
            headBloodBar.text.text = $"{Property.Health}/{Property.HealthMax}";
            headBloodBar.image.fillAmount = (float)Property.Health / Property.HealthMax;

            bullet = Object.FindAnyObjectByType<SceneManager>().actorViews[2];
            firePoint = View.transform.Find("firepoint");
            rigidbody = View.GetComponent<Rigidbody>();
        }

        protected override void SyncPosition(in Operation opt)
        {
            if (opt.identity == ClientBase.Instance.UserId)
                return;
            base.SyncPosition(opt);
        }

        protected override ActorView GetViewPrefab()
        {
            return Object.FindAnyObjectByType<SceneManager>().actorViews[0];
        }

        public override void Fire(in Operation opt)
        {
            var bullet1 = Object.Instantiate(bullet, firePoint.position, firePoint.rotation);
            var rigidbody = bullet1.GetComponent<Rigidbody>();
            rigidbody.AddForce(firePoint.forward * 1000f);
            var bullet2 = bullet1.GetComponent<Bullet>();
            bullet2.target = this;
            Object.Destroy(bullet1, 5f);
        }

        public override void Resurrection(in Operation opt)
        {
            headBloodBar.gameObject.SetActive(true);
            UpdateBloodBar();
        }

        private void DeadDelay()
        {
            headBloodBar.gameObject.SetActive(false);
            GameEvent.OnPlayerDead?.Invoke();
        }

        public void OnCollisionEnter(Collision collision)
        {
            if (collision.collider.name == "PlayGround")
                isGround = true;
        }

        public void OnCollisionExit(Collision collision)
        {
            if (collision.collider.name == "PlayGround")
                isGround = false;
        }

        public override void OnUpdate()
        {
            if (Identity == ClientBase.Instance.UserId)
            {
                var dir = Camera.main.transform.Transform3Dir(InputJoystick.Direction);
                if (dir != Vector3.zero)
                {
                    View.transform.rotation = Quaternion.Lerp(View.transform.rotation, Quaternion.LookRotation(dir, Vector3.up), 0.5f);
                    View.transform.Translate(0, 0, 10f * Time.deltaTime);
                }
                AddOperation(new Operation(Command.SyncPosition, Identity, View.transform.position, View.transform.rotation));
            }
            else
            {
                base.OnUpdate();
            }
        }
    }
}
#endif